<?php
class Cart {
    /*
    购物车数据结构：
    [
        ['id'=>n, 'num'=>m],
        ['id'=>n, 'num'=>m],
        ['id'=>n, 'num'=>m],
        ...
    ]
    用购物车类来维护购物车数据的session
    */
    
    // 初始化
    function __construct()
    {
        
    }
    function __destruct()
    {
        
    }

    /**
     * 添加一个新的商品数组
     */
    function add(array $arr)
    {
        if (is_array($arr)) {
            if (!empty($_SESSION['cartdata'])) {
                // 已经存在于购物车中，改变数量
                if ($this->checkExisted($arr['id'])) {
                    if ($this->changeNum($arr['id'])) {
                        return true;
                    } else {
                        echo 'change failed';
                
                        return false;
                    }
                } else {
                    $_SESSION['cartdata'][] = $arr;
                    return true;
                }
            } else { // 第一次添加到购物车
                $_SESSION['cartdata'][] = $arr;
                return true;
            }
        } else {
            return false;
        }
    }

    /**
     * 检测是否存在相同的数据
     */
    private function checkExisted($id)
    {
        if (!empty($id)) {
            #var_dump($id);
            #var_dump($_SESSION['cartdata']);
            
            foreach ($_SESSION['cartdata'] as $key=>$value) {
                if ($id == $value['id']) {
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 改变数量
     */
    private function changeNum($id, $forward = true)
    {
        if ($forward) { // 加数量
            var_dump($id);
            var_dump($_SESSION['cartdata']);
            
            foreach ($_SESSION['cartdata'] as $key=>$value) {
                if ($id == $value['id']) {
                    $_SESSION['cartdata'][$key]['num'] += 1;
                    return true;
                }
            }
            echo 'no same';
            return false;
        }else{//减数量
            foreach ($_SESSION['cartdata'] as $key=>$value) {
                if ($id == $value['id']) {
                    $_SESSION['cartdata'][$key]['num'] -= 1;
                    if ($_SESSION['cartdata'][$key]['num'] < 1) {
                        $this->delItem($id);
                    }
                    return true;
                }
            }
            return false;
        }
    }

    /**
     * 删除购物车中的商品
     */
    function delItem($id)
    {
        if (empty($_SESSION['cartdata'])) {
            return false;
        }
        foreach ($_SESSION['cartdata'] as $key=>$value) {
            if ($id == $value['id']) {
                unset($_SESSION['cartdata'][$key]);
                return true;
            }
        }
        return false;
    }

    function del($id) 
    {
        return $this->changeNum($id, false);
    }

    function increase($id) 
    {
        return $this->changeNum($id, true);
    }

    function getData()
    {
        if (empty($_SESSION['cartdata'])) {
            return array();
        }
        return $_SESSION['cartdata'];
    }
    
    function getCount($id)
    {
        if (empty($_SESSION['cartdata'])) {
            return 0;
        }
        foreach ($_SESSION['cartdata'] as $key=>$value) {
            if ($id == $value['id']) {
                return $_SESSION['cartdata'][$key]['num'];
            }
        }
        return 0;
    }

    function clear()
    {
        $_SESSION['cartdata'] = null;
    }

    /**
     * 计算商品和购物车信息，得出最终结果。
     */
    function calcData(array $goods_array)
    {
        $data_array = $this->getData();
        $result_arr = array();
        if (!empty($data_array) && !empty($goods_array)) {
            foreach ($data_array as $cart_row) {
                foreach ($goods_array as $goods_row) {
                    $cart_name = $cart_row['id'];
                    $cart_count = $cart_row['num'];
                    $goods_name = $goods_row['name'];
                    $goods_salary = $goods_row['salary'];
                    
                    $total = $goods_salary * $cart_count;
                    if ($cart_name == $goods_name) {
                        $arr = array(
                            'name'=>$cart_name,
                            'count'=>$cart_count,
                            'salary'=>$goods_salary,
                            'total'=>$total
                        );
                        $result_arr[] = $arr;
                    }
                }              
            }    
        }
        return $result_arr;
    }
};